<?php
	/*
	 * File: datab_user_api.php
	 * Last modification: 21.07.2011
	 * Author(s): kivio.wanderley
	 * 
	 */
?>

<?php /** CENTER PHP CODE */
	
	/* ========================================
	 * Selects
	 * ========================================
	 */

	function datab_user_select( $p_id = null ) {
		/* Select from teacher */
		$t_query = "SELECT user AS username, nivel AS access_level, usuario_id AS id
					FROM tb_login
					WHERE usuario_id = $p_id;";
		$t_result = datab_query($t_query);	
			
		# If result got en error
		if(is_string($t_result)) { set_info_msg($t_result, I_ERR); }
		
		# If result got more than one result
		if( sizeof($t_result) > 1 ) { set_info_msg("MORE THAN ONE RESULT ON USER SELECT", I_ERR); }
		
		return $t_result[0];
	}
	
	function datab_user_get_status($p_user_id)  {	 
		if($p_status) $t_status = "TRUE"; else $t_status = "FALSE";
		
		$t_query = "SELECT status AS status
					FROM tb_login
					WHERE usuario_id = $p_user_id;";
		$t_result = datab_query($t_query);
		if(is_string($t_result)) { set_info_msg($t_result, I_ERR); }
		
		return $t_result[0]['status'] == 't' ? true : false;
	}
	
	/* ========================================
	 * Inserts
	 * ========================================
	 */
	
	/**
	 * Insert a user from a person.
	 * @param (string) $p_nome Username;
	 * @param (string) $p_codigo User pass code;
	 * @param (int) $p_nivel Access level;
	 * @param (int) $fk_p_id Person Id for its user;
	 */
	function datab_user_insert($fk_p_id, $p_nome, $p_codigo, $p_nivel)  {	 
		$t_query = "INSERT INTO tb_login (usuario_id, user, pass, nivel, status)
					VALUES ($fk_p_id, $p_nome, $p_codigo, $p_nivel, 1);"; 
		$t_row = datab_query($t_query);
		if(is_string($t_row)) { return false; }
		
		return true;
	}
	
	/* ========================================
	 * Misc
	 * ========================================
	 */

	/**
	 * Change the status value of user
	 * @param (int) $p_user_id
	 * @param (bool) $p_status
	 */
	function datab_user_change_status($p_user_id, $p_status = false)  {	 
		if($p_status) $t_status = "TRUE"; else $t_status = "FALSE";
		
		$t_query = "UPDATE tb_login
					SET status = $t_status
					WHERE usuario_id = $p_user_id;";
		$t_row = datab_query($t_query);
		if(is_string($t_row)) { set_info_msg($t_row, I_ERR); return false; }
		
		return true;
	}
?>